import java.io.*;


/**
 * FileStreaemTest
 *
 * @author byc
 * @date 2022-04-21
 */
public class FileStreaemTest {
    public static void main(String[] args) {
        InputStream is = null;
        try {
            is = new FileInputStream("C:\\Users\\admin\\Desktop\\本草.md");

            Reader reader = new InputStreamReader(is,"utf-8");
            char[] chars = new char[4];

            byte[] bytes = new byte[16];

            //选择方法
            Boolean method = false;
            if (method){
                //方法一 简单 byte流
                while (true){
                    int readCount = is.read(bytes);
                    if (readCount == -1){
                        break;
                    }
                    System.out.print(new String(bytes,0,readCount));
                }
            } else {
                //方法二 快捷 字符流
                int readData;
                while ((readData = reader.read(chars)) != -1){
                    System.out.print(new String(chars,0,readData));
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //在finally语句块中确保流一定关闭
            try {
                if (is != null){
                    is.close();
                }
            }catch(IOException e){}
        }
    }
}
